package borge.web.teamspirit.client.widgets;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.IndepSerie;
import borge.web.teamspirit.client.data.Serie;
import borge.web.teamspirit.client.data.SerieEnums;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class CreateSerieWidget extends CreateDataWidget implements ClickHandler {

    private IndepSerie iSerie;
    
    public CreateSerieWidget(WidgetEventHandler aEventHandler, IndepSerie aSerie){
        super(aEventHandler);
    
        iSerie = aSerie;
        
        if(aSerie == null)
            aSerie = new IndepSerie();
        // We should be able to improve this beyonds textboxes..
        
        TeamSpiritConstants lsc = TeamSpirit.getConstants();
        int row = 0;
        final int maxLength = 10;
        
        addTableRow(row++, new InputWrapper(lsc.season(), createTextBox(aSerie.getSeason(), maxLength), true));
        addTableRow(row++, new InputWrapper(lsc.division(), createTextBox(aSerie.getDivision(), maxLength), true));
        //addTableRow(row++, new InputWrapper(lsc.start_date(), createDatePicker(aSerie.getStartDate()), true));
        addTableRow(row++, new InputWrapper(lsc.start_date(), createDateBox(aSerie.getStartDate()), true));
        addTableRow(row++, new InputWrapper(lsc.end_date(), createDateBox(aSerie.getEndDate()), true));
        
        addTableRow(row++, new InputWrapper(lsc.placement(), createTextBox(aSerie.getPlacement(), maxLength), false));
        addArrayTableRow(row++, new InputWrapper(lsc.type(), SerieEnums.Type.values(), null, getSelectedType(aSerie.getType()), true));
        addArrayTableRow(row++, new InputWrapper(lsc.status(), SerieEnums.Status.values(), null, getSelectedStatus(aSerie.getStatus()), true));
        
        // We want to listen to ListBox events for Status, so we can enable/disable Placement
        // textbox depending on serie status
        
        addButton(row, 0, this);
        iPanel.add(iTable);
        initWidget(iPanel);
    }
    
    private int getSelectedType(String aType){
       
        if(aType == null)
            return 0;
        
        SerieEnums.Type type = SerieEnums.Type.valueOf(aType);
        return type.ordinal();
    }
    
    private int getSelectedStatus(String aStatus){
        
        if(aStatus == null)
            return 0;
        
        SerieEnums.Status status = SerieEnums.Status.valueOf(aStatus);
        return status.ordinal();
    }
    
    @SuppressWarnings("unchecked")
    public Serie getDataObject(){
        return iSerie;
    }
    
    public void onClick(ClickEvent aEvent){
        
        postEvent(WidgetEventType.SERIE_CREATED);
        
    }
    
}
